﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.NetworkInformation;
using System.Net;


namespace SQLH2ConfigTool.Util
{
    public class PingUtil
    {
        public bool PingServer(string ServerName)
        {
            try
            {
                Ping pingSender = new Ping();
                PingOptions options = new PingOptions();

                // Use the default Ttl value which is 128,
                // but change the fragmentation behavior.
                options.DontFragment = true;

                // Create a buffer of 32 bytes of data to be transmitted.
                string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
                byte[] buffer = Encoding.ASCII.GetBytes(data);
                int timeout = 5;
                PingReply reply = pingSender.Send(ServerName, timeout, buffer, options);
                if (reply.Status != IPStatus.Success)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                _Error = ex;
                return false;
            }
        }

        private Exception _Error;
        public Exception Error
        {
            get { return _Error; }
        }
    }
}
